cd "$Mydirectory1/1_DataSources/NLSY79/"

//read in data file
	clear all
	set maxvar 10000
	insheet using ./rawdata/marstat_expanded.csv, clear comma

//for some reason, var names are uncapitalized when brought in. Need to be capitalized so that nls-provided cleanup will work
	ren r* R*
	ren t* T*
	
* do nls-provided cleanup *

label define vlR0173600 1 "CROSS MALE WHITE"  2 "CROSS MALE WH. POOR"  3 "CROSS MALE BLACK"  4 "CROSS MALE HISPANIC"  5 "CROSS FEMALE WHITE"  6 "CROSS FEMALE WH POOR"  7 "CROSS FEMALE BLACK"  8 "CROSS FEMALE HISPANIC"  9 "SUP MALE WH POOR"  10 "SUP MALE BLACK"  11 "SUP MALE HISPANIC"  12 "SUP FEM WH POOR"  13 "SUP FEMALE BLACK"  14 "SUP FEMALE HISPANIC"  15 "MIL MALE WHITE"  16 "MIL MALE BLACK"  17 "MIL MALE HISPANIC"  18 "MIL FEMALE WHITE"  19 "MIL FEMALE BLACK"  20 "MIL FEMALE HISPANIC" 
label values R0173600 vlR0173600

label define vlR0214700 1 "HISPANIC"  2 "BLACK"  3 "NON-BLACK, NON-HISPANIC" 
label values R0214700 vlR0214700

label define vlR0214800 1 "MALE"  2 "FEMALE" 
label values R0214800 vlR0214800

label define vlR0217501 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R0217501 vlR0217501

label define vlR0405601 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R0405601 vlR0405601

label define vlR0618601 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R0618601 vlR0618601

label define vlR0898401 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R0898401 vlR0898401

label define vlR1144901 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R1144901 vlR1144901

label define vlR1520101 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R1520101 vlR1520101

label define vlR1890801 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R1890801 vlR1890801

label define vlR2257901 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R2257901 vlR2257901

label define vlR2445301 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R2445301 vlR2445301

label define vlR2871000 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R2871000 vlR2871000

label define vlR3074700 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R3074700 vlR3074700

label define vlR3401400 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R3401400 vlR3401400

label define vlR3656800 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R3656800 vlR3656800

label define vlR4007300 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  5 "REMARRIED"  6 "WIDOWED" 
label values R4007300 vlR4007300

label define vlR4418400 0 "NEVER MARRIED"  1 "MARRIED"  2 "SEPARATED"  3 "DIVORCED"  6 "WIDOWED" 
label values R4418400 vlR4418400

label define vlR5081400 0 "0: 0  NEVER MARRIED"  1 "1: 1  MARRIED"  2 "2: 2  SEPARATED"  3 "3: 3  DIVORCED"  6 "6: 6  WIDOWED" 
label values R5081400 vlR5081400

label define vlR5166700 0 "0: NEVER MARRIED"  1 "1: MARRIED"  2 "2: SEPARATED"  3 "3: DIVORCED"  6 "6: WIDOWED" 
label values R5166700 vlR5166700

label define vlR6479300 0 "Never Married"  1 "Married"  2 "Separated"  3 "Divorced"  6 "Widowed" 
label values R6479300 vlR6479300

label define vlR7007000 0 "Never Married"  1 "Married"  2 "Separated"  3 "Divorced"  6 "Widowed" 
label values R7007000 vlR7007000

label define vlR7704300 0 "0: 0  NEVER MARRIED"  1 "1: 1  MARRIED"  2 "2: 2  SEPARATED"  3 "3: 3  DIVORCED"  6 "6: 6  WIDOWED" 
label values R7704300 vlR7704300

label define vlR8496700 0 "0: 0  NEVER MARRIED"  1 "1: 1  MARRIED"  2 "2: 2  SEPARATED"  3 "3: 3  DIVORCED"  6 "6: 6  WIDOWED" 
label values R8496700 vlR8496700

label define vlT0988500 0 "0: 0  NEVER MARRIED"  1 "1: 1  MARRIED"  2 "2: 2  SEPARATED"  3 "3: 3  DIVORCED"  6 "6: 6  WIDOWED" 
label values T0988500 vlT0988500

label define vlT2210500 0 "0: 0  NEVER MARRIED"  1 "1: 1  MARRIED"  2 "2: 2  SEPARATED"  3 "3: 3  DIVORCED"  6 "6: 6  WIDOWED" 
label values T2210500 vlT2210500

label define vlT3108400 0 "0: 0  NEVER MARRIED"  1 "1: 1  MARRIED"  2 "2: 2  SEPARATED"  3 "3: 3  DIVORCED"  6 "6: 6  WIDOWED" 
label values T3108400 vlT3108400

label define vlT4112900 0 "0: 0  NEVER MARRIED"  1 "1: 1  MARRIED"  2 "2: 2  SEPARATED"  3 "3: 3  DIVORCED"  6 "6: 6  WIDOWED" 
label values T4112900 vlT4112900

label define vlT5023300 0 "0: 0  NEVER MARRIED"  1 "1: 1  MARRIED"  2 "2: 2  SEPARATED"  3 "3: 3  DIVORCED"  6 "6: 6  WIDOWED" 
label values T5023300 vlT5023300

label define vlT5771200 0 "0: 0  NEVER MARRIED"  1 "1: 1  MARRIED"  2 "2: 2  SEPARATED"  3 "3: 3  DIVORCED"  6 "6: 6  WIDOWED" 
label values T5771200 vlT5771200
 
  rename R0000100 CASEID_1979 
  rename R0173600 SAMPLE_ID_1979 
  rename R0214700 SAMPLE_RACE_78SCRN 
  rename R0214800 SAMPLE_SEX_1979 
  rename R0217501 MARSTAT_KEY_1979   // MARSTAT-KEY
  rename R0405601 MARSTAT_KEY_1980   // MARSTAT-KEY
  rename R0618601 MARSTAT_KEY_1981   // MARSTAT-KEY
  rename R0898401 MARSTAT_KEY_1982   // MARSTAT-KEY
  rename R1144901 MARSTAT_KEY_1983   // MARSTAT-KEY
  rename R1520101 MARSTAT_KEY_1984   // MARSTAT-KEY
  rename R1890801 MARSTAT_KEY_1985   // MARSTAT-KEY
  rename R2257901 MARSTAT_KEY_1986   // MARSTAT-KEY
  rename R2445301 MARSTAT_KEY_1987   // MARSTAT-KEY
  rename R2871000 MARSTAT_KEY_1988   // MARSTAT-KEY
  rename R3074700 MARSTAT_KEY_1989   // MARSTAT-KEY
  rename R3401400 MARSTAT_KEY_1990   // MARSTAT-KEY
  rename R3656800 MARSTAT_KEY_1991   // MARSTAT-KEY
  rename R4007300 MARSTAT_KEY_1992   // MARSTAT-KEY
  rename R4418400 MARSTAT_KEY_1993   // MARSTAT-KEY
  rename R5081400 MARSTAT_KEY_1994   // MARSTAT-KEY
  rename R5166700 MARSTAT_KEY_1996   // MARSTAT-KEY
  rename R6479300 MARSTAT_KEY_1998   // MARSTAT-KEY
  rename R7007000 MARSTAT_KEY_2000   // MARSTAT-KEY
  rename R7704300 MARSTAT_KEY_2002   // MARSTAT-KEY
  rename R8496700 MARSTAT_KEY_2004   // MARSTAT-KEY
  rename T0988500 MARSTAT_KEY_2006   // MARSTAT-KEY
  rename T2210500 MARSTAT_KEY_2008   // MARSTAT-KEY
  rename T3108400 MARSTAT_KEY_2010   // MARSTAT-KEY
  rename T4112900 MARSTAT_KEY_2012   // MARSTAT-KEY
  rename T5023300 MARSTAT_KEY_2014   // MARSTAT-KEY
  rename T5771200 MARSTAT_KEY_2016   // MARSTAT-KEY

//replace values less than 0 (i.e., refusal, don't know, invalid skip, valid skip, or non-interview) as "."
	foreach num of numlist 79/93 94(2)98 0(2)16 {
		
		if inrange(`num',79,98) local mil 19
		if inrange(`num',0,16) local mil 20
		
		local yy: display %02.0f `num' //if local num only has 1 digit, put a 0 in front of it
			
		tab MARSTAT_KEY_`mil'`yy', m
		replace MARSTAT_KEY_`mil'`yy' =. if MARSTAT_KEY_`mil'`yy'<0
		tab MARSTAT_KEY_`mil'`yy', m
		
		ren MARSTAT_KEY_`mil'`yy' marital_status_expanded`mil'`yy'
		label var marital_status_expanded`mil'`yy' "R's marital status, expanded, `mil'`yy'"
		
	}
	
//drop unnecessary vars
	drop SAMPLE_ID_1979 SAMPLE_RACE_78SCRN SAMPLE_SEX_1979	
	
//save data
	sort CASEID_1979 // uniquely identifies Rs
	compress
	save ./rawdata/marstat_expanded.dta, replace

